<?php
/** 
 * 权限规则模型扩展类
 *
 * @author   xg
 * @date     2019-09-17
 */

namespace app\home\model;

use think\Db;
use think\Model;
use think\facade\Log;
use app\common\model\AuthRule;

class AuthRuleModel extends AuthRule
{

    /**
     * 获取后台菜单
     * @return  array   菜单数据
     */
    public function getMenu()
    {
        $data = $this
            ->where('status = '.parent::STATUS_1)
            ->where('is_menu = '.parent::IS_MENU_1)
            ->select();

        if ($data->isEmpty()) {
            return;
        }

        foreach ($data->toArray() as $value) {
            $rule_map[$value['rule_id']] = [
                'rule_id'   => $value['rule_id'],
                'pid'       => $value['pid'],
                'path'      => $value['path'],
                'route'     => $value['route'],
                'title'     => $value['title'],
                'icon'      => $value['icon'],
                'is_menu'   => $value['is_menu'],
                'is_node'   => $value['is_node'],
                'child'     => [],
            ];
        }

        foreach ($rule_map as $key => $value) {
            if ($value['pid'] > 0) {
                $rule_map[$value['pid']]['child'][] = &$rule_map[$key];
            }
        }

        foreach ($rule_map as $key => $value) {
            if($value['pid'] > 0){
                unset($rule_map[$key]);
                continue;
            }

            $result[] = $value;
        }
        
        return $result;
    }

}
